﻿Imports Data
' Usamos el Imports para que podamos crear Threads
Imports System.Threading


Public Class Deudas
    Private Property id_usuario As Integer

   
    Private Sub lkNueva_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lkNueva.LinkClicked
        Try
            Dim nueva_deuda As New Data.Deudas With {.id_empleado = id_usuario}

            Dim deuda As New IngresoDeuda(nueva_deuda, IngresoDeuda.Acciones.Ingresar)
            deuda.ShowDialog()
            deuda.Dispose()
            Me.CargarLVDeuda()
        Catch ex As Exception

        End Try
    End Sub

    Public Sub Deudas_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.CargarCBXMeses()
        Me.CargarCBXEstados()
        Me.CargarLVDeuda()
    End Sub

    Public Function CargarLVDeuda() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim id_estado As Integer = Me.cbxEstado.SelectedValue
                Dim n_mes As Integer = Me.cbxMes.SelectedValue

                Dim lista As List(Of Data.Deudas) = (From x In db.Deudas _
                                                  Where x.id_estado = id_estado And _
                                                  x.fecha_pago.Value.Month = n_mes _
                                                  Select x).ToList
                Me.lvDeudas.Items.Clear()
                Dim total As Double = lista.Sum(Function(p) p.total)
                For Each item In lista
                    Me.lvDeudas.Items.Add(New ListViewItem(New String() {item.id, item.nombre, item.descripcion, item.fecha_pago, item.total}) With {.Name = item.id})
                Next
                Me.lvDeudas.Items.Add(New ListViewItem(New String() {String.Empty, String.Empty, String.Empty, String.Empty, total}) With {.Name = "none"})

            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Function CargarCBXMeses() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim lista As List(Of AbstractObject) = (From x In db.Meses _
                                                     Select New AbstractObject With {.ID = x.id, _
                                                                                     .Nombre = x.nombre}).Distinct.ToList

                Dim manager As BindingManagerBase = Me.cbxMes.BindingContext(lista)

                manager.SuspendBinding()
                Me.cbxMes.DisplayMember = "Nombre"
                Me.cbxMes.ValueMember = "ID"
                Me.cbxMes.DataSource = lista
                Me.cbxMes.Enabled = True
                manager.ResumeBinding()

            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Function CargarCBXEstados() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext


                Dim lista As List(Of AbstractObject) = (From x In db.Estado _
                                                        Where x.id_tipo_estado = 1 _
                                                        Select New AbstractObject With {.ID = x.id, _
                                                                                        .Nombre = x.nombre}).Distinct.ToList

                Dim manager As BindingManagerBase = Me.cbxEstado.BindingContext(lista)

                manager.SuspendBinding()
                Me.cbxEstado.DisplayMember = "Nombre"
                Me.cbxEstado.ValueMember = "ID"
                Me.cbxEstado.DataSource = lista
                Me.cbxEstado.Enabled = True
                manager.ResumeBinding()

            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Public Sub New()
        InitializeComponent()
    End Sub

    Public Sub New(ByVal id As Integer)

        ' This call is required by the designer.
        InitializeComponent()
        Me.id_usuario = id

    End Sub

    Private Sub ToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem2.Click
        Try
            Dim deuda As Data.Deudas = Me.ObtenerDatosDeuda(Me.lvDeudas.SelectedItems(0).Name)
            Dim m_deuda As New IngresoDeuda(deuda, IngresoDeuda.Acciones.Modificar)
            m_deuda.ShowDialog()
            Me.CargarLVDeuda()
        Catch ex As Exception

        End Try
    End Sub

    Private Function ObtenerDatosDeuda(ByVal id As Integer) As Data.Deudas
        Try
            Using db As New SerigraphicaDBDataContext
                Dim deuda As Data.Deudas = db.Deudas.Where(Function(p) p.id = id).Select(Function(p) p).FirstOrDefault
                Return deuda
            End Using
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Private Sub ToolStripMenuItem1_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripMenuItem1.Click
        Try
            If Me.EliminarDeuda(Me.lvDeudas.SelectedItems(0).Name) Then
                MsgBox("Eliminacion Exitosa")
                Me.CargarLVDeuda()
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Function EliminarDeuda(ByVal id As Integer) As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim deuda As Data.Deudas = db.Deudas.Where(Function(p) p.id = id).Select(Function(p) p).FirstOrDefault
                db.Deudas.DeleteOnSubmit(deuda)
                db.SubmitChanges()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub PagarDeudaToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles PagarDeudaToolStripMenuItem.Click
        Try
            If Me.CancelarDeuda(Me.lvDeudas.SelectedItems(0).Name) Then
                MsgBox("Deuda Cancelada")
                Me.CargarLVDeuda()
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Function CancelarDeuda(ByVal id As Integer) As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim deuda As Data.Deudas = db.Deudas.Where(Function(p) p.id = id).Select(Function(p) p).FirstOrDefault
                deuda.id_estado = 1
                db.SubmitChanges()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub btnRefrescar_Click(sender As System.Object, e As System.EventArgs) Handles btnRefrescar.Click
        Try
            If Me.CargarLVDeuda() Then
                MsgBox("ok")
            End If

        Catch ex As Exception

        End Try
    End Sub
End Class